﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace CastleDefender.Platform.TouchBehavior
{
    public class TouchBehavior : DrawableGameComponent
    {
        #region VARIABLE

        protected Vector2 vecPosPress;
        protected Vector2 vecPosMove;
        protected bool blnIsPressed;
        protected bool blnIsMoved;
        protected bool blnIsZoomedIn;
        protected bool blnIsZoomedOut;
        protected int intZoomLevel;

        #endregion 

        #region CONSTRUCTOR

        public TouchBehavior(Game myGame):base (myGame)
        {
            blnIsMoved = blnIsPressed = blnIsZoomedIn = blnIsZoomedOut = false;
            intZoomLevel = 0;
        }

        #endregion 

        #region PUBLIC METHOD

        public override void Update(GameTime gameTime)
        {
            TouchCollection touchCollection = TouchPanel.GetState();

            foreach (TouchLocation touchLocation in touchCollection)
            {
                //nếu như trước đó chưa press thì sẽ chuyển sang trạng thái pressed
                if (!blnIsPressed && touchLocation.State == TouchLocationState.Pressed)
                {
                    blnIsPressed = true;
                    vecPosPress = touchLocation.Position;
                } else 
                //nếu trước đó là trạng thái Pressed thì sẽ chuyển sang Moved
                if (blnIsPressed && touchLocation.State == TouchLocationState.Moved)
                {
                    blnIsPressed = false;
                    blnIsMoved = true;
                    vecPosMove = touchLocation.Position;
                }
                else
                    //Trả lại trạng thái false khi tay nhấc khỏi sự kiện phím.
                    if (touchLocation.State == TouchLocationState.Released)
                    {
                        blnIsMoved = false;
                        blnIsPressed = false;
                    }
            }
            base.Update(gameTime);
        }

        #endregion 

        #region PUBLIC PROPERTIES

        public Vector2 PosPress
        {
            get { return vecPosPress; }
            private set{}
        }

        public Vector2 PosMove
        {
            get { return vecPosMove; }
            private set { }
        }

        public bool IsPressed
        {
            get { return blnIsPressed; }
            private set { }
        }

        public bool IsMoved
        {
            get { return blnIsPressed; }
            private set { }
        }

        public bool IsZoomedIn
        {
            get { return blnIsZoomedIn; }
            private set { }
        }

        public bool IsZoomedOut
        {
            get { return blnIsZoomedIn; }
            private set { }
        }

        public int ZoomLevel
        {
            get { return intZoomLevel; }
            set { intZoomLevel = value; }
        }

        #endregion 

        #region PROTECTED METHOD


        #endregion
    }
}
